package br.uefs.dexa.rfframework.gui.impl;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import br.uefs.dexa.rfframework.config.AppConfigManager;
import br.uefs.dexa.rfframework.gui.interfaces.OutputObjectSetContainerPanel;
import br.uefs.dexa.rfframework.gui.interfaces.RFUserGUI;
import br.uefs.dexa.rfframework.gui.utils.ScreenUtils;
import br.uefs.dexa.rfframework.gui.utils.WrapLayout;

public class ImageSetVisualizationPanel extends OutputObjectSetContainerPanel {


	private static final long serialVersionUID = 8494355446188516088L;

	
	private static final float PROGRESS_BAR_LABEL_FONT_SIZE = 18.0f;
	
	private static final int PROGRESS_BAR_WIDTH = 300;
	
	private static final int PROGRESS_BAR_HEIGTH = 30;
	
	private JDialog dialog;
	
	private JProgressBar progressBar;
	
	private int min;
	
	private int max;
	
	
	public ImageSetVisualizationPanel(RFUserGUI gui) 
	{
		super(gui);
	}
	
	@Override
	public void compose() 
	{
		setLayout(new WrapLayout());
	}

	@Override
	public Container getOuputObjectsContainer() 
	{
		return this;
	}

	@Override
	public void startLoadingProgressViewer(int min, int max)
	{
		this.min = min;
		this.max = max;
		dialog = new JDialog(null, "", Dialog.ModalityType.DOCUMENT_MODAL);
		dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		progressBar = new JProgressBar(min, max);
		//setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		setGlobalCursor();
		
		//if(max > PROGRESS_BAR_TRESHOLD) {
		if(max > AppConfigManager.getInstance().getProgressBarTreshhold()) {
			Thread t = new Thread(new Runnable() {
				
				@Override
				public void run() {
					
					
					JPanel dialogPanel = new JPanel();
					//dialogPanel.setLayout(new BoxLayout(dialogPanel, BoxLayout.PAGE_AXIS));
					dialogPanel.setLayout(new BorderLayout());
					//dialogPanel.setLayout(new WrapLayout());
					
					dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
					//dialogPanel.setPreferredSize(new Dimension(200, 100));
					JLabel label = new JLabel("Loading images", SwingConstants.CENTER);
					label.setFont (label.getFont ().deriveFont (PROGRESS_BAR_LABEL_FONT_SIZE));
					label.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
					dialogPanel.add(label, BorderLayout.NORTH);
					
					dialogPanel.add(progressBar);
			        progressBar.setValue(0);
			        progressBar.setStringPainted(true);
			        
			        progressBar.setPreferredSize(new Dimension(PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGTH));
			        progressBar.setMaximumSize(new Dimension(PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGTH));
			        
			        dialog.setModal(true);
			    	//dialog.add(progressBar);
			        dialog.add(dialogPanel, BorderLayout.SOUTH);
			    	dialog.pack();
			    	//dialog.setLocation((ScreenUtils.getWidth())/2 - dialog.getWidth()/2 , (ScreenUtils.getHeigth())/2 - dialog.getHeight()/2);
			    	dialog.setLocation((ScreenUtils.getHalfWidth() - dialog.getWidth()/2) + 35 , (ScreenUtils.getHalfHeigth() - dialog.getHeight()/2));
			    	//dialog.setUndecorated(true);
			    	dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			    	dialog.setTitle("RFEngine");
			    	dialog.setVisible(true);
				}
			});
			t.start();
		}
		
		
	}
	
	private void setGlobalCursor()
	{
		Container container = getParent();
		while(container.getParent() != null) {
			container = container.getParent();
		}
		
		container.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	}
	
	private void resetGlobalCursor()
	{
		Container container = getParent();
		while(container.getParent() != null) {
			container = container.getParent();
		}
		container.setCursor(null);
	}
	
	@Override
	public void updateLoadingProgressViewer(int progress)
	{
		//if (max > PROGRESS_BAR_TRESHOLD) {
		if (max > AppConfigManager.getInstance().getProgressBarTreshhold()) {
			Thread t2 = new Thread(new Runnable() {
				@Override
				public void run() {
					progressBar.setValue(progress);
				}
			});
			t2.start();
		}
	}
	
	@Override
	public void finalizeLoadingProgressViewer()
	{
		setCursor(null); //turn off the wait cursor
		resetGlobalCursor();
		dialog.setVisible(false);
	}


}
